/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
 * right(right) {}
 * };
 */
class Solution {
public:
    void insert(TreeNode*& root, vector<int>& nums, int left, int right) {
        if (left <= right) {
            int mid = (left + right) / 2;
            root = new TreeNode(nums[mid]);
            insert(root->left, nums, left, mid - 1);
            insert(root->right, nums, mid + 1, right);
        }
    }
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        struct TreeNode* root;
        insert(root, nums, 0, nums.size() - 1);
        return root;
    }
};
